﻿using Business.Core.Annotations;
using Business.Data;
using Common;
using DataModel;
using LinqToDB;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys
{
    public class TaskOverTimeMerber : Common.BusinessBase
    {
        [Doc("新增任务加班", Group = "任务加班")]
        public virtual async Task<dynamic> AddTaskOverTime(string arg)
        {
            var flag = 0;
            string message = "";
            try
            {
                MANAGER_TASK_OVERTIME oModel = JsonConvert.DeserializeObject<MANAGER_TASK_OVERTIME>(arg);
                AuthInfo authinfo = Logins.Authorize();
                var arr = oModel.TASKID.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (arr.Length > 0)
                {
                    foreach (var item in arr)
                    {
                       
                        using (var con = Data.DB.GetConnection())
                        {
                            var oTask = con.MANAGER_TASK.Where(c=>c.ID==item).FirstOrDefault();
                            oModel.TASKID = item;
                            oModel.ACTIVE = 1;
                            oModel.CREATENAME = authinfo.userid;
                            oModel.CREATETIME = DateTime.Now;
                            oModel.DEPARTCODE = authinfo.departid;
                            oModel.REALNAME = authinfo.realname;
                            oModel.ID = Guid.NewGuid().ToString();
                            oModel.APPROVALSTATUS = 0;
                            flag += await con.InsertAsync(oModel);
                            if (flag >= 1) message = "添加成功";
                            //审批信息入库
                            string information = "任务标题为：" + oTask.TASKNAME + "的任务加班已提交，请审批";
                            new LogDataOperation().noticeLog(6, oModel.APPROVALUSERID, oModel.TASKID, information, "MANAGER_TASK");
                        }

                    }
                    
                }
                

            }
            catch (Exception ex)
            {
                message = "添加失败，请联系技术人员";
            }
            var obj = new { flag = flag, message = message };
            //入库的用户
            return JsonConvert.SerializeObject(obj);
        }

        [Doc("获取任务加班列表", Group = "任务加班")]
        public virtual async Task<dynamic> GetOverTimeListByID(string taskid)
        {
            List<MANAGER_TASK_OVERTIME> list = new List<MANAGER_TASK_OVERTIME>();
            AuthInfo authinfo = Logins.Authorize();
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    list = con.MANAGER_TASK_OVERTIME.Where(c => c.ACTIVE == 1 && c.TASKID == taskid).ToList();
                }
            }
            catch (Exception ex) { }

            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return JsonConvert.SerializeObject(list, Formatting.Indented, timeFormat);
        }

        [Doc("获取任务加班内容", Group = "任务加班")]
        public virtual async Task<dynamic> GetOverTimeDetailByID(string id)
        {
            MANAGER_TASK_OVERTIME list = new MANAGER_TASK_OVERTIME();
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    list = con.MANAGER_TASK_OVERTIME.Where(c => c.ID == id).FirstOrDefault();
                }
            }
            catch (Exception ex) { }

            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return JsonConvert.SerializeObject(list, Formatting.Indented, timeFormat);
        }

        [Doc("加班审批", Group = "任务加班")]
        public virtual async Task<dynamic> ApprovalTimeDetailByID(string arg)
        {
            int flag = 0;
            string msg = "";
            try
            {
                AuthInfo authinfo = Logins.Authorize();
                using (var con = Data.DB.GetConnection())
                {
                    var oTime = JsonConvert.DeserializeObject<MANAGER_TASK_OVERTIME>(arg);
                    var oModel = con.MANAGER_TASK_OVERTIME.Where(c => c.ID == oTime.ID).FirstOrDefault();
                    if (oModel != null)
                    {
                        oModel.APPROVALSTATUS = oTime.APPROVALSTATUS;
                        oModel.APPROVALTIME = DateTime.Now;
                        oModel.APPROVALREASON = oTime.APPROVALREASON;
                        oModel.APPROVALLASTNAME = authinfo.realname;
                        flag = await con.UpdateAsync(oModel);

                        //修改任务需要的实际时长
                        string SQL = string.Format("update MANAGER_TASK set REALTIME = nvl(REALTIME,0) + {0},OVERTIMES = nvl(OVERTIMES,0) + {0} where id='{1}'", oModel.TIMES,oModel.TASKID);
                        con.ExecuteNonQuery(SQL);
                    }
                    else msg = "未查询到数据";
                }
            }
            catch (Exception ex) { msg = "审批失败，请联系技术人员"; }
            var obj = new { flag = flag,msg = msg};
            return JsonConvert.SerializeObject(obj);
        }
    }
}
